﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eConfiguration : EditBase
    {
        #region Public Members

        public bool IsNewItem
        {
            get { return (ActiveItemId == Guid.Empty); }
        }

        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }

        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;

            if (ActiveItemId != Guid.Empty)
            {
                bindItem();
            }

        }


        #endregion

      

        #region Helpers

        private void bindItem()
        {
            ConfigurationController cc = new ConfigurationController();
            ConfigurationInfo ci = cc.SelectConfiguration(ActiveItemId);
            if (ci != null)
            {
                lblHeaderRight.Text = ci.Name;
                txtName.Text = ci.Name;
                txtDescription.Text = ci.Description;
                chkIsDefault.Checked = ci.IsDefault;
            }
        }

        #endregion

        #region Action Event Helpers

        private void SaveItem()
        {
            if (Page.IsValid)
            {
                ConfigurationController cc = new ConfigurationController();
                ConfigurationInfo ci = new ConfigurationInfo();



                if (!IsNewItem)
                    ci = cc.SelectConfiguration(ActiveItemId);

                ci.Name = txtName.Text;
                ci.Description = txtDescription.Text;
                ci.IsDefault = chkIsDefault.Checked;

                if (ci.IsDefault)
                {
                    ResetDefault();
                }

                if (IsNewItem)
                {
                    cc.InsertConfiguration(ci);
                }
                else
                {
                    cc.UpdateConfiguration(ci);
                }

                RaiseNavEvent(this, new NavEventArgs("eConfigurations", ActiveItemId));
            }
        }

        private void ResetDefault()
        {
            ConfigurationController cc = new ConfigurationController();
            List<ConfigurationInfo> ciList = cc.SelectConfigurations();
            foreach (ConfigurationInfo ci in ciList)
            {
                if (ci.IsDefault)
                {
                    ci.IsDefault = false;
                    cc.UpdateConfiguration(ci);
                }
            }

        }

        public void CopyItem(Guid id)
        {
            ConfigurationController cc = new ConfigurationController();
            ConfigurationInfo ci = cc.SelectConfiguration(id);
            if (ci != null)
            {
                ci.Name = ci.Name + " copy";
                ci.IsDefault = false;
                Guid newId = cc.InsertConfiguration(ci);
                ConfigurationSettingController csc = new ConfigurationSettingController();
                List<ConfigurationSettingInfo> csiList = csc.SelectConfigurationSettingsByConfiguration(id);
                foreach (ConfigurationSettingInfo csi in csiList)
                {
                    csi.ConfigurationId = newId;
                    csc.InsertConfigurationSetting(csi);
                }

                RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", newId, newId));

            }

        }

        public void DeleteItem(Guid id)
        {
            ConfigurationController cc = new ConfigurationController();
            ConfigurationInfo ci = cc.SelectConfiguration(id);
            if (ci != null)
            {
                if (!ci.IsDefault)
                {
                    cc.DeleteConfiguration(id);
                    RaiseNavEvent(this, new NavEventArgs("eConfigurations", id));
                }
            }

        }
        #endregion

        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Save":
                    SaveItem();
                    break;
                case "Delete":
                    DeleteItem(ActiveItemId);
                    break;
                case "Copy":
                    CopyItem(ActiveItemId);
                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", ActiveItemId, ActiveItemId));
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Save", "Save Changes", "~/images/icons/save.png", "", true, true));
                al.Add(new ActionInfo("Copy", "Copy Configuration", "~/images/icons/copy.png", "", true, false));
                al.Add(new ActionInfo("Delete", "Delete Configuration", "~/images/icons/delete.png", "Deleting this setting will also remove it from any configurations. Are you sure you wish to delete this setting?", !IsNewItem));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));
                return al;
            }
        }

        #endregion
    }
}